package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SupplierBean;
import com.project.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("supplier")
@Api(tags = "供应商控制器")
public class SupplierController {
    @Autowired
    private ISupplierService service;

    @RequiresPermissions("data:supplier")
    @RequestMapping("cutByItem")
    @ApiOperation(value = "供应商分页", notes = "分页展示供应商")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNO", value = "页码", required = true),
            @ApiImplicitParam(name = "sName", value = "供应商名称"),
            @ApiImplicitParam(name = "sRange", value = "供应商经营范围")
    })
    public IPage<SupplierBean> cutByItem(Integer pageNO, String sName, String sRange) {
        return service.cutByItem(pageNO, sName, sRange);
    }

    @RequiresPermissions("data:supplier")
    @RequestMapping("findBySupplierId")
    @ApiOperation(value = "根据供应商id查询供应商", notes = "根据供应商id查询供应商")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "supplierId", value = "供应商id", required = true),
    }
    )
    public SupplierBean findBySupplierId(Integer supplierId) {
        return service.findBySupplierId(supplierId);
    }

    @RequiresPermissions("data:supplier")
    @RequestMapping("updateSupplier")
    @ApiOperation(value = "根据供应商id修改供应商负责人和负责人电话", notes = "根据供应商id修改供应商负责人和负责人电话")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "supplierId", value = "供应商id", required = true),
            @ApiImplicitParam(name = "sLeader", value = "供应商负责人"),
            @ApiImplicitParam(name = "sLeaderPhone", value = "供应商负责人电话"),
    }
    )
    public String updateSupplier(Integer supplierId, String sLeader, String sLeaderPhone) {
        service.updateSupplier(supplierId, sLeader, sLeaderPhone);
        return "ok";
    }

    @RequiresPermissions("data:supplier")
    @RequestMapping("addSupplier")
    @ApiOperation(value = "添加供应商", notes = "添加供应商")
    public String addSupplier(SupplierBean supplier) {
        service.addSupplier(supplier);
        return "ok";
    }

    @RequiresPermissions("data:supplier")
    @RequestMapping("addSupplierList")
    @ApiOperation(value = "添加供应商Excel", notes = "添加供应商Excel")
    public String addSupplierList(@RequestParam("excelFile") MultipartFile mf) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        params.setSheetNum(1);
        List<SupplierBean> supplierList = ExcelImportUtil.importExcel(mf.getInputStream(),
                SupplierBean.class, params);
        service.addSupplierList(supplierList);
        return "ok";
    }

    @RequiresPermissions("data:supplier")
    @RequestMapping("findAllSupplier")
    @ApiOperation(value = "查询所有供应商", notes = "查询所有供应商")
    public List<SupplierBean> findAllSupplier() {
        return service.findAllSupplier();
    }
}
